#include <string.h>
#include <stdio.h>
#include "mhscpu.h"


void NVIC_Configuration(void);
void UART_Configuration(void);
void SENSOR_Configuration(void);

void clearBPK(void);
void setBPK(void);
void printBPK(void);
void SENSOR_EnableStatePrintf(void);
int main(void)
{
	uint32_t i;
	SYSCTRL_PLLConfig(SYSCTRL_PLL_96MHz);
	SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div_None);
	SYSCTRL_PCLKConfig(SYSCTRL_PCLK_Div2);
	
	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_BPU | SYSCTRL_APBPeriph_GPIO,ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_BPU,ENABLE);
	
	UART_Configuration();
	
	printf("MegaHunt SCPU SENSOR Lock Demo V1.0.\r\n");
	
	printf("Disable All Sensor\n");
	SENSOR_ANACmd(SENSOR_ANA_VOL_HIGH|
						SENSOR_ANA_VOL_LOW|
						SENSOR_ANA_TEMPER_HIGH|
						SENSOR_ANA_TEMPER_LOW|
						SENSOR_ANA_XTAL32K |
						SENSOR_ANA_MESH |
						SENSOR_ANA_VOLGLITCH , DISABLE);
	SENSOR_EXTCmd(DISABLE);
	for(i = 0; i < 12; i++)
	{
		SENSOR->SEN_EN[i] = 0x55;
	}
	SENSOR_EnableStatePrintf();
	
	printf("Enable All Sensor\n");
	SENSOR_ANACmd(SENSOR_ANA_VOL_HIGH|
						SENSOR_ANA_VOL_LOW|
						SENSOR_ANA_TEMPER_HIGH|
						SENSOR_ANA_TEMPER_LOW|
						SENSOR_ANA_XTAL32K |
						SENSOR_ANA_MESH |
						SENSOR_ANA_VOLGLITCH , ENABLE);
	SENSOR_EXTCmd(ENABLE);
	for(i = 0; i < 12; i++)
	{
		SENSOR->SEN_EN[i] = 0xAA;
	}
	SENSOR_EnableStatePrintf();
	
	printf("Lock All Sensor\n");
	SENSOR_Lock(SENSOR_LOCK_EXTS|
						SENSOR_LOCK_VOL_HIGH|
						SENSOR_LOCK_VOL_LOW|
						SENSOR_LOCK_TEMPER_HIGH|
						SENSOR_LOCK_TEMPER_LOW |
						SENSOR_LOCK_VOLGLITCH |
						SENSOR_LOCK_XTAL32K |
						SENSOR_LOCK_MESH );
	
	printf("Try to Disable All Sensor\n");
	SENSOR_ANACmd(SENSOR_ANA_VOL_HIGH|
						SENSOR_ANA_VOL_LOW|
						SENSOR_ANA_TEMPER_HIGH|
						SENSOR_ANA_TEMPER_LOW|
						SENSOR_ANA_XTAL32K |
						SENSOR_ANA_MESH |
						SENSOR_ANA_VOLGLITCH , DISABLE);
	SENSOR_EXTCmd(DISABLE);
	for(i = 0; i < 12; i++)
	{
		SENSOR->SEN_EN[i] = 0x55;
	}
	SENSOR_EnableStatePrintf();
 
	while(1)
	{

	}
}


void SENSOR_EnableStatePrintf(void)
{
	const char*SENSOR_NAME[19] = {"SENSOR_EN_EXT0", 
						"SENSOR_EN_EXT1",
						"SENSOR_EN_EXT2",
						"SENSOR_EN_EXT3",
						"SENSOR_EN_EXT4",
						"SENSOR_EN_EXT5",
						"SENSOR_EN_EXT6",
						"SENSOR_EN_EXT7",
						"SENSOR_EN_EXT8",
						"SENSOR_EN_EXT9",
						"SENSOR_EN_EXT10",
						"SENSOR_EN_EXT11",
						"SENSOR_EN_VH",
						"SENSOR_EN_VL",
						"SENSOR_EN_TH",
						"SENSOR_EN_TL",
						"SENSOR_EN_XTAL32",
						"SENSOR_EN_MESH",
						"SENSOR_EN_VOLGLITCH"
					};
	uint32_t i;
	for(i = 0; i < 19; i++)
	{
		printf("	%s value %08X\n", SENSOR_NAME[i], SENSOR->SEN_EN[i]);
	}
	printf("	SENSOR_EN_EXTS value %08X\n", SENSOR->SEN_EXTS_START);
}

void clearBPK(void)
{
	uint32_t buf[32];
	uint32_t index = 0;
	
	while(BPK_IsReady() == RESET);
	
	//clear buf
	memset(buf, 0, sizeof(buf));

	printf("bufValue:\n");
	for(index = 0;index < sizeof(buf)/sizeof(buf[0]);index++)
	{
		printf("%08X ", buf[index]);
		if (3 == index % 4)
		{
			printf("\n");
		}
	}
	printf("\n");
	BPK_WriteKey(buf,sizeof(buf)/sizeof(buf[0]),0);
}

void setBPK(void)
{
	uint32_t buf[32];
	uint32_t index = 0;
	
	while(BPK_IsReady() == RESET);
	
	//set buf 0~X
	for(index = 0;index < sizeof(buf)/sizeof(buf[0]);index++)
	{
		buf[index] = index;
	}

	printf("bufValue:\n");
	for(index = 0;index < sizeof(buf)/sizeof(buf[0]);index++)
	{
		printf("%08X ", buf[index]);
		if (3 == index % 4)
		{
			printf("\n");
		}
	}
	printf("\n");
	BPK_WriteKey(buf,sizeof(buf)/sizeof(buf[0]),0);
}

void printBPK(void)
{
	uint32_t key[32];
	uint32_t index = 0;
	
	while(BPK_IsReady() == RESET);

	printf("readKey:\n");
	memset(key, 0, sizeof(key));
	BPK_ReadKey(key,sizeof(key)/sizeof(key[0]),0);
	for(index = 0;index < sizeof(key)/sizeof(key[0]);index++)
	{
		printf("%08X ", key[index]);
		if (3 == index % 4)
		{
			printf("\n");
		}
	}
	printf("\n");
	memset(key, 0, sizeof(key));
}


void SENSOR_IRQHandler(void)
{
	printf("SENSOR_IRQHandler in %08X\n",SENSOR->SEN_STATE);
	SENSOR->SEN_STATE = 0;

	printBPK();
	setBPK();
	printBPK();
	
	NVIC_ClearPendingIRQ(SENSOR_IRQn);
}

void SSC_IRQHandler(void)
{
	printf("SSC_IRQHandler in %08X\n",SSC->SSC_SR);
	SSC_ClearITPendingBit(SSC_ITSysXTAL12M|SSC_ITSysGlitch|SSC_ITSysVolHigh|SSC_ITSysVolLow);

	printBPK();
	setBPK();
	printBPK();
	
	NVIC_ClearPendingIRQ(SENSOR_IRQn);
}


void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_3);
	
	NVIC_InitStructure.NVIC_IRQChannel = SENSOR_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	NVIC_InitStructure.NVIC_IRQChannel = SSC_IRQn;
	NVIC_Init(&NVIC_InitStructure);
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;
	
	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 |GPIO_Pin_1, GPIO_Remap_0);	
	
	UART_InitStructure.UART_BaudRate = 115200;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;
	
	UART_Init(UART0, &UART_InitStructure);
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif


